//
//  VistorView.swift
//  weibo
//
//  Created by tomato on 2021/9/9.
//

import UIKit

// MARK: - 访客视图的协议
 
protocol VistorViewDelegate:NSObjectProtocol {
    
    // 2.定义协议方法
    /// 注册按钮被点击后的代理方法
    func visitorViewDidClickRegister()
    
    /// 登录按钮被点击后的代理方法
    func visitorViewDidClickLogin()
    
}

//-处理访客未登录视图界面显示
class VistorView: UIView {

    //设置代理属性
    weak var delegate: VistorViewDelegate?
    
    
    // MARK: - 按钮点击事件
    // 按钮点击事件方法是系统来调用, 当把按钮点击事件private后系统就找不到这个方法了
    // @objc: 让系统可以找到我们的方法
    
    @objc private func didClickRegister() {
//       print(#function)
        // view不能Modal出控制器来,需要将事件传递给控制器
        // 4.调用代理的方法
        delegate?.visitorViewDidClickRegister()
    }
    
    @objc private func didClickLogin() {
//        print(#function)
        // 4.调用代理的方法
        delegate?.visitorViewDidClickLogin()
    }
    
    // MARK: - 公开的方法
    /// 设置访客视图内容
    func setupVisitorInfo(imageName: String?, message: String) {
        
        // 设置messageLabel
        messageLabel.text = message
        guard let imgName = imageName else {
            
            //播放动画
            startRotationAnimation()
            return
        }
        
        // 将转轮图片替换
        iconView.image = UIImage(named:imgName)
        
        // 隐藏小房子
        homeView.isHidden = true
        sendSubviewToBack(coverView)
    }
    
    /// 转轮旋转动画
    func startRotationAnimation() {
        
        // 创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = Double.pi * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 核心动画完成的时候不要移除
        anim.isRemovedOnCompletion = false
        // 将核心动画添加到图层上面
        iconView.layer.add(anim, forKey: nil)
    }
    
    
    /// 这个构造函数通过xib/sb里面创建才会走这个方法
    /// UIKit的里面的view即可以通过代码来创建,也可以通过xib/sb来创建
    /// required 表示必须实现的构造函数有required不需要加override
    required init?(coder aDecoder: NSCoder) {
        
        //如果使用xib创建视图，会直接崩掉，阻止xib创建视图
        fatalError("init(coder:) has not been implemented")
    }
    
    // override: 构造函数,看父类有没有和这个一样的构造函数
    override init(frame: CGRect) {
        // 在super.init之前给属性赋值
        //调用对象方法不能放到super.init之前,在super.init之前,这个对象还没初始化好(创建),对象没创建好是不能调用对象的方法的
        super.init(frame: frame)
        
        setupUI()
    }
    
    // MARK: - 懒加载控件
    /// 转轮：
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息文字
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.text = "关注一些人,看看有什么惊喜!"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 16)
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        // 设置
        button.setTitle("注册", for: UIControl.State.normal)
//        button.titleLabel?.text  不要这样设置,到时候出不来
        button.setTitleColor(UIColor.orange, for: UIControl.State.normal)
        //图片需要拉伸，去Assets去找到图片，右下方找到Show Slicing去设置拉伸
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControl.State.normal)
        button.addTarget(self, action: #selector(self.didClickRegister), for: UIControl.Event.touchUpInside)
        return button
        
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        
        
        let button = UIButton()
        // 设置
        button.setTitle("登录", for: UIControl.State.normal)
        button.setTitleColor(UIColor.darkGray, for: UIControl.State.normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControl.State.normal)
        button.addTarget(self, action: #selector(self.didClickLogin), for: UIControl.Event.touchUpInside)
        return button
        
    }()
    /// 遮盖视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
}

// MARK: -  设置界面
extension VistorView{
    
    private func setupUI(){
        
        // 设置背景颜色
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //添加控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
     
        //设置布局，添加约束
        for v in subviews {
            
            //默认是true,false 表示支持使用自动布局来设置控件位置
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        /// CenterX和父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 要添加的View的属性
        // toItem: 要参照的view
        // attribute: 要参照View的属性
        // 将约束添加到view上面去
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
        /// CenterY和父控件的CenterY重合
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: -40))
        
        // 小房子
        // CenterX转轮CenterX
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: iconView, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
        // CenterY转轮CenterY
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: iconView, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0))
        
        
        // 消息label
        /// 宽度
        /// 当参照的view为nil时,属性为NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX 和父控件CenterX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
        
        // Top和转轮底部
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: iconView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 16))
        
        
        // 注册按钮
        /// 顶部和label底部
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: messageLabel, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 16))
        
        /// 左边和label对齐
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: messageLabel, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0))
        
        /// 宽度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 35))
        
        
        // 登录按钮
        /// 顶部和label底部
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: messageLabel, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 16))
        
        /// 右边和label对齐
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: messageLabel, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0))
        
        /// 宽度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 35))
        
        /// 遮盖视图
//        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0))
//        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: registerButton, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0))

        
        /*
         VFL:可视化格式语言
         H 水平方向
         V 垂直方向
         | 边界
         []  包装控件
         views:是一个字典[名字：控件名] -VFL 字符串中表示控件的字符串
         metrics: 是一个字典[名字：NSNumber] - VFL 字符串中表示某一个数值
         */
        
//        水平约束
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views:["mask" : coverView]))
        // 垂直约束
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":-36], views:["mask" : coverView,"regButton":registerButton]))
    }

    
}
